import sys

from qgis.PyQt import QtWidgets
from qgis.PyQt import uic

# sys.path.append('../')
from tool.utils import *
from algorithm.tif2tfw_algorithm import *

# if __name__ != '__main__':
#     from tif2tfw_algorithm import *
# else:
#     from algorithm.tif2tfw_algorithm import *

# This loads your .ui file so that PyQt can populate your plugin with the elements from Qt Designer
FORM_CLASS, _ = uic.loadUiType(os.path.join(
    Ui_path, 'one_file_input_template.ui'))


class tif2tfwDialog(QtWidgets.QDialog, FORM_CLASS):
    def __init__(self, parent=None):
        """Constructor."""
        super(tif2tfwDialog, self).__init__(parent)
        # Set up the user interface from Designer through FORM_CLASS.
        # After self.setupUi() you can access any designer object by doing
        # self.<objectname>, and you can use autoconnect slots - see
        # http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html
        # #widgets-and-dialogs-with-auto-connect
        self.setupUi(self)

        self.setWindowTitle('tif2tfw')
        self.label.setText('tif文件地址：')
        self.mQgsFileWidget.setFilter("TIF文件(*.tif)")
        self.buttonBox.accepted.connect(self.run)
        print(self.tab_class())

    def tool_name(self):
        return '生成tfw文件'

    def tab_class(self):
        return '无人机'

    def run(self):
        result = Tif2tfw_algorithm(self.mQgsFileWidget.filePath())
        if result['is_ok'] == True:
            QtWidgets.QMessageBox.information(
                self,
                '操作成功',
                f"操作成功\n{result['output_path']}")
        else:
            QtWidgets.QMessageBox.critical(
                self,
                '操作失败',
                f'操作失败\n{result}')
        print(result)


if __name__ == '__main__':
    from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QPlainTextEdit

    app = QApplication([])
    dlg = tif2tfwDialog()

    dlg.show()
    app.exec_()
